using System;

namespace NUnit.Behave
{
    public class WhenContext : Context
    {
        private ThenContext thenContext;

        public WhenContext() : this(new ThenContext())
        {
        }

        public WhenContext(ThenContext thenContext)
        {
            this.thenContext = thenContext;
            singleMessageType = MessageType.When;
            singleMessageTypePending = MessageType.When_Pending;
            conjunctionMessageType = MessageType.When_And;
            conjunctionMessageTypePending = MessageType.When_And_Pending;
        }

        public WhenContext Add_AddVerb<T>(string whenMessage, T actionValue)
        {
            return EvaluateMessageForCatalogEntry(this, whenMessage, actionValue, MessageType.When_Pending);
        }

        public WhenContext And<T>(string whenMessage, T actionValue)
        {
            return EvaluateMessageForCatalogEntry(this, whenMessage, actionValue, MessageType.When_And_Pending);
        }

        public WhenContext Add_AddVerb<T>(string whenMessage, T actionValue, Action<T> delegateAction)
        {
            return EnvokeActionAndStoreForLaterUse(this, actionValue, delegateAction, whenMessage, MessageType.When);
        }

        public WhenContext And<T>(string whenMessage, T actionValue, Action<T> delegateAction)
        {
            return
                EnvokeActionAndStoreForLaterUse(this, actionValue, delegateAction, whenMessage, MessageType.When_And);
        }

        public ThenContext Then<T>(string thenMessage, T actionValue)
        {
            thenContext.Add_AddVerb(thenMessage, actionValue);
            return thenContext;
        }

        public ThenContext Then<T>(string thenMessage, T actionValue, Action<T> delegateAction)
        {
            thenContext.Add_AddVerb(thenMessage, actionValue, delegateAction);

            return thenContext;
        }

        protected override void PassToNextProvider(IMessageProvider provider)
        {
            thenContext.RenderTo(provider);
        }
    }
}